
package com.hxrk.aidlservice;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;

import com.hxrk.cecardvr.LogUtils;

import static com.hxrk.cecardvr.MyApplication.mainPackage;
import static com.hxrk.cecardvr.MyApplication.mainPackages;


public class DvrServiceManager {
    private static final String TAG = "DvrServiceManager";

    public CarDVRCallBack mDvrCallBack;
    private static DvrServiceManager instance;
    private static Context mContext;
    public boolean bServiceConn = false;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogUtils.e(TAG, "onServiceConnected");
            mDvrCallBack = CarDVRCallBack.Stub.asInterface(service);
            bServiceConn = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtils.e(TAG, "onServiceDisconnected");
            bServiceConn = false;
            bindMpospService(mContext);
        }

    };

    synchronized public static DvrServiceManager getInstance() {
        if (instance == null) {
            instance = new DvrServiceManager();
        }
        return instance;
    }

    public boolean bindMpospService(Context context) {
        mContext = context;
        // 绑定远程服务
        Intent intent = new Intent();
        PackageManager pmc = mContext.getPackageManager();
        Intent intentmain = null;
        for (int i = 0; i < mainPackages.length; i++){
            intentmain = pmc.getLaunchIntentForPackage(mainPackages[i]);
            if (intentmain != null){
                mainPackage = mainPackages[i];
                break;
            }
        }

        intent.setClassName(mainPackage, "com.hxrk.aidlservice.CEDidiMainService");
        // 与服务端进行绑定，绑定成功会回调onServiceConnected里面
//        while (!context.bindService(intent, mConnection, Service.BIND_AUTO_CREATE)
//                /*&& conCount++ < 30*/) {
//            LogUtils.e(TAG, "context.bindService 失败");
//            conCount++;
//            try {
//                Thread.sleep(2000);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            if (conCount % 10 == 0 && intentmain != null)
//                mContext.startActivity(intentmain);
//        }
//        return true;
        if (!mContext.bindService(intent, mConnection, Service.BIND_AUTO_CREATE | Context.BIND_IMPORTANT)){
            LogUtils.d(TAG, "context.bindService 失败");
            bServiceConn = false;
            bindMpospService(mContext);
        }else {
            LogUtils.d(TAG, "context.bindService 成功");
            return true;
        }
        return false;
    }

    public void unbindMposService(Context context) {
        context.unbindService(mConnection);
    }
}
